﻿<template>
	<div class="mrsExchangeOrder-container">
		<el-dialog v-model="state.isShowDialog" draggable width="769px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit />
					</el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" size="default" label-width="80px" :rules="rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="产品包" prop="packageId">
							<!-- <el-input v-model="state.ruleForm.packageId" placeholder="请输入产品包ID" class="w100" clearable /> -->
							<el-select v-model.trim="state.ruleForm.packageId" placeholder="请选择" class="w100">
								<el-option :label="item.label" :value="item.value"
									v-for="item, index in state.SysMrsPackageList" :key="index"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="产品名称" prop="productId">
							<!-- <el-input v-model="state.ruleForm.productId" placeholder="请输入产品Id" class="w100" clearable /> -->
							<el-select v-model.trim="state.ruleForm.productId" placeholder="请选择" class="w100">
								<el-option :label="item.label" :value="item.value"
									v-for="item, index in state.SysMrsProductApiList" :key="index"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="提供商" prop="providerId">
							<!-- <el-input v-model="state.ruleForm.providerId" placeholder="请输入提供商Id" class="w100" clearable /> -->
							<el-select v-model.trim="state.ruleForm.providerId" placeholder="请选择" class="w100">
								<el-option :label="item.label" :value="item.value"
									v-for="item, index in state.SysMrProviderApiList" :key="index"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="外部Id" prop="outOrderId">
							<el-input v-model="state.ruleForm.outOrderId" placeholder="请输入外部Id，我方生成提供对方的唯一标识" class="w100"
								clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="提供方Id" prop="providerOrderId">
							<el-input v-model="state.ruleForm.providerOrderId" placeholder="提供方返回来的ID，与外部ID相对应" class="w100"
								clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="商品Id" prop="providerProductId">
							<el-input v-model="state.ruleForm.providerProductId" placeholder="请输入提供方商品Id" class="w100"
								clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="产品别名" prop="productAlias">
							<el-input v-model="state.ruleForm.productAlias" placeholder="请输入产品别名" class="w100" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="实际价格" prop="productRealPrice">
							<el-input-number v-model="state.ruleForm.productRealPrice" placeholder="请输入实际价格" class="w100"
								clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户ID" prop="userId">
							<el-input v-model="state.ruleForm.userId" placeholder="请输入用户ID" class="w100" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="手机号" prop="phone">
							<el-input v-model="state.ruleForm.phone" placeholder="请输入手机号" class="w100" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="订单状态" prop="exchageOrderStatus">
							<!-- <el-input v-model="state.ruleForm.exchageOrderStatus" placeholder="请输入订单兑换状态" class="w100"
								clearable />	 -->
							<el-select v-model.trim="state.ruleForm.exchageOrderStatus" placeholder="请选择" class="w100">
								<el-option label="待付款" :value="0"></el-option>
								<el-option label="已付款" :value="1"></el-option>
								<el-option label="已完成" :value="2"></el-option>
								<el-option label="已取消" :value="3"></el-option>
								<el-option label="待退款" :value="4"></el-option>
								<el-option label="已退款" :value="5"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="权益卡号" prop="cardNo">
							<el-input v-model="state.ruleForm.cardNo" placeholder="请输入会员权益卡号" class="w100" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="权益密码" prop="cardPassWord">
							<el-input v-model="state.ruleForm.cardPassWord" placeholder="请输入会员权益卡密码" class="w100"
								clearable />
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="失效时间" prop="expireTime">
							<!-- <el-input v-model="state.ruleForm.expireTime" placeholder="请输入会员权益卡失效时间" class="w100"
								clearable /> -->
							<el-date-picker v-model.trim="state.ruleForm.expireTime" type="datetime" placeholder="选择日期"
								date-format="YYYY/MM/DD" time-format="hh:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
								style="width: 100%" :default-time="defaultDate" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="延时兑换" prop="delayMinute">
							<el-input v-model="state.ruleForm.delayMinute" placeholder="请输入延时兑换分钟" class="w100" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="请求应答" prop="responseData">
							<el-input v-model="state.ruleForm.responseData" placeholder="请输入请求应答数据" class="w100" clearable
								type="textarea" :autosize="{ minRows: 2 }" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="通知应答" prop="callbackData">
							<el-input v-model="state.ruleForm.callbackData" placeholder="请输入通知应答数据" class="w100" clearable
								type="textarea" :autosize="{ minRows: 2 }" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="权益链接" prop="cardLink">
							<el-input v-model="state.ruleForm.cardLink" placeholder="请输入会员权益卡链接" class="w100" clearable
								type="textarea" :autosize="{ minRows: 2 }" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="state.ruleForm.remark" placeholder="请输入备注" class="w100" clearable
								type="textarea" :autosize="{ minRows: 2 }" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel" size="default">取 消</el-button>
					<el-button type="primary" @click="submit" size="default">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="editMrsExchangeOrder" >
import { reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import type { FormRules } from "element-plus";
import mittBus from '/@/utils/mitt';
import SysMrsPackageApi from '/@/api/memberRights/package';
import { MrsExchangeOrder } from '/@/api/models';
import SysMrsExchangeOrderApi from '/@/api/memberRights/exchangeOrder';
import SysMrsProductApi from '/@/api/memberRights/product';
import SysMrProviderApi from '/@/api/memberRights/provider';
import { getNowTime } from '/@/utils/formatTime';

// 定义变量内容
const props = defineProps({
	title: String,
});
const ruleFormRef = ref();
const state = reactive({
	isShowDialog: false,
	ruleForm: {} as MrsExchangeOrder,
	SysMrsPackageList: [] as any,
	SysMrsProductApiList: [] as any,
	SysMrProviderApiList: [] as any,
});

// 刚进入页面给的默认当前日期
const defaultDate = getNowTime();

// 删除非必填规则
const rules = ref<FormRules>({
	packageId: [{ required: true, message: '请输入产品包ID！', trigger: 'blur', },],
	productId: [{ required: true, message: '请输入产品Id！', trigger: 'blur', },],
	providerId: [{ required: true, message: '请输入提供商Id！', trigger: 'blur', },],
});

// 打开弹窗
const openDialog = async (row: MrsExchangeOrder) => {
	state.ruleForm = row;
	state.isShowDialog = true;
	await resSysMrsPackageApi();
	await resSysMrsProductApi();
	await resSysMrProviderApi();
};

// 关闭弹窗
const closeDialog = () => {
	mittBus.emit('submitRefresh');
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 提交
const submit = () => {
	ruleFormRef.value.validate(async (valid: boolean, fields?: any) => {
		if (valid) {
			let res;
			if (state.ruleForm.id != undefined && state.ruleForm.id > 0)
				res = await SysMrsExchangeOrderApi().editPost(state.ruleForm);
			else
				res = await SysMrsExchangeOrderApi().addPost(state.ruleForm);
			if (res.data.resCode != '0000')
				ElMessage.error(res.data.resMessage);
			else {
				ElMessage.success("操作成功");
				closeDialog();
			}
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

// 产品包列表
const resSysMrsPackageApi = async () => {
	await SysMrsPackageApi().queryAll().then((res) => {
		if (res.data.resCode === '0000')
			state.SysMrsPackageList = res.data.result ?? [];
	});
}

// 产品列表
const resSysMrsProductApi = async () => {
	await SysMrsProductApi().queryAll().then((res) => {
		if (res.data.resCode === '0000')
			state.SysMrsProductApiList = res.data.result ?? [];
	});
}

// 提供商列表
const resSysMrProviderApi = async () => {
	await SysMrProviderApi().queryAll().then((res) => {
		if (res.data.resCode === '0000')
			state.SysMrProviderApiList = res.data.result ?? [];
	});
}

// 导出对象 将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
